


package testy;

import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;



public class Ewus {

	JavascriptExecutor javascriptExecutor() {
		return (JavascriptExecutor) driver;
	}
	private WebDriver driver;
	//private Utils2 u;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	//private WebDriverBackedSelenium selenium;
	
	@Before
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		//driver = new HtmlUnitDriver();
		baseUrl = "https://nfz2.kamsoft.pl";
		//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		
		
	}
	
	
/*	@Test
	public void testLogujEWUS() throws Exception {
		driver.get(baseUrl + "/ap-mzwi-reg/reg/regmod/regaptwizadd@default+start");
		//selenium =  new WebDriverBackedSelenium(driver, "");
	}*/
	

	@Test
	public void testWerPESEL() throws Exception {
		driver.get(baseUrl + "/ap-ewus/");
		driver.switchTo().frame("info");
		
		driver.findElement(By.name("FFFRAXlogin")).sendKeys("150005220");
		driver.findElement(By.name("FFFRAXpasw")).sendKeys("abcABC123*");
		driver.findElement(By.name("sub1")).click();
		
		waitForElementPresent(By.id("idbbf0a4ff_FFFRIQ11-pesel_control"));
		javascriptExecutor().executeScript("$('#idbbf0a4ff_FFFRIQ11-pesel_control').kmsk('value','22222222222');");

		int i=0;
		int y=10;
		int k=0;
		
		while (i<y)
		{
			if(driver.findElement(By.name("SEARCH_BUTTON")).isEnabled())
			{
				driver.findElement(By.name("SEARCH_BUTTON")).click();
				i++;
				k++;
			}
			else
			{
				i++;
			}
			
		}
		
		System.out.println("LICZNIK:     "+k);
		
		
		
		
	}
	
	
		
	@After
	public void tearDown() throws Exception {
		//driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
	
	public void waitForElementPresent(final By by) throws Exception {
		try {
			new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
	            public Boolean apply(WebDriver webDriver) {
	                return isElementPresent(by);
	            }
	        });
		} catch (NoSuchElementException e) {
			throw new Exception("Nie znaleziono elementu: " + by.toString());
		}
	}
	
	public void waitForElementNotPresent(final By by) throws Exception {
		try {
			
			new WebDriverWait(driver, 10, 1)
			.until(new ExpectedCondition<Boolean>() {
				int licznik=0;
				public Boolean apply(WebDriver d) {
					Boolean czy_obecny = isElementPresent(by);
					System.out.println("Licznik: "+licznik);
					licznik++;
					if(!czy_obecny) return true;
					else return false;
				}
			});
			
			
		} catch (NoSuchElementException e) {
			throw new Exception("Nie znaleziono elementu: " + by.toString());
		}
	}
	
	public boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
}
